package cn.yuanqiao.archive.formManage.formRule.service.impl;

import cn.yuanqiao.archive.formManage.formRule.domain.YqFormRule;
import cn.yuanqiao.archive.formManage.formRule.mapper.YqFormRuleMapper;
import cn.yuanqiao.archive.formManage.formRule.service.IYqFormRuleService;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分单规则Service业务层处理
 * 
 * @author yuanqiao
 * @date 2023-06-26
 */
@Service
public class YqFormRuleServiceImpl implements IYqFormRuleService
{
    @Autowired
    private YqFormRuleMapper yqFormRuleMapper;

    /**
     * 查询分单规则
     * 
     * @param ruleId 分单规则主键
     * @return 分单规则
     */
    @Override
    public YqFormRule selectYqFormRuleByRuleId(String ruleId)
    {
        return yqFormRuleMapper.selectYqFormRuleByRuleId(ruleId);
    }

    /**
     * 查询分单规则列表
     * 
     * @param yqFormRule 分单规则
     * @return 分单规则
     */
    @Override
    public List<YqFormRule> selectYqFormRuleList(YqFormRule yqFormRule)
    {
        return yqFormRuleMapper.selectYqFormRuleList(yqFormRule);
    }

    /**
     * 查询分单规则列表
     *
     * @param yqFormRule 分单规则
     * @return 分单规则
     */
    @Override
    public List<YqFormRule> listAndNoTenant(YqFormRule yqFormRule)
    {
        return yqFormRuleMapper.listAndNoTenant(yqFormRule);
    }


    /**
     * 新增分单规则
     * 
     * @param yqFormRule 分单规则
     * @return 结果
     */
    @Override
    public int insertYqFormRule(YqFormRule yqFormRule)
    {
        return yqFormRuleMapper.insertYqFormRule(yqFormRule);
    }

    /**
     * 修改分单规则
     * 
     * @param yqFormRule 分单规则
     * @return 结果
     */
    @Override
    public int updateYqFormRule(YqFormRule yqFormRule)
    {
        return yqFormRuleMapper.updateYqFormRule(yqFormRule);
    }

    @Override
    public boolean updateColumnConditon(String formId) {
        YqFormRule queryWrapper = new YqFormRule();
        queryWrapper.setFormId(formId);
        queryWrapper.setType("0");
        List<YqFormRule> yqFormRules = selectYqFormRuleList(queryWrapper);
        for (YqFormRule yqFormRule : yqFormRules) {
            List<YqFormRule> childRules = selectYqFormRuleByGroupId(yqFormRule.getRuleId());
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < childRules.size(); i++) {
                YqFormRule item = childRules.get(i);
                if (i != 0) {
                    stringBuilder.append(" ").append(item.getColumnCondition().replace("and", "&&").replace("or", "||")).append(" ");
                }else {
                    stringBuilder.append("${");
                }
                String fieldName = item.getFieldName();
                String condition = item.getCONDITION();
                String dynamicValue = item.getDynamicValue();
                String staticValue = item.getStaticValue();
                String isStatic = item.getIsStatic();

                // 根据条件生成EL表达式
                String elExpression;
                if (condition.equals("equals")) {
                    elExpression = fieldName + " == '" + (isStatic.equals("1") ? staticValue : dynamicValue) + "'";
                } else if (condition.equals("contains")) {
                    elExpression = "fn:contains(" + fieldName + ", '" + (isStatic.equals("1") ? staticValue : dynamicValue) + "')";
                } else if (condition.equals("startsWith")) {
                    elExpression = "fn:startsWith(" + fieldName + ", '" + (isStatic.equals("1") ? staticValue : dynamicValue) + "')";
                } else if (condition.equals("endsWith")) {
                    elExpression = "fn:endsWith(" + fieldName + ", '" + (isStatic.equals("1") ? staticValue : dynamicValue) + "')";
                } else if (condition.equals("notContains")) {
                    elExpression = "not fn:contains(" + fieldName + ", '" + (isStatic.equals("1") ? staticValue : dynamicValue) + "')";
                }  else {
                    // 默认情况下使用原始的条件和值
                    elExpression = fieldName + condition + (isStatic.equals("1") ? staticValue : dynamicValue);
                }

                stringBuilder.append(elExpression);
            }
            stringBuilder.append("}");

            String generatedExpression = stringBuilder.toString();
            System.out.println("=========================");
            System.out.println(generatedExpression);
            YqFormRule ruleGroup = new YqFormRule();
            ruleGroup.setRuleId(yqFormRule.getRuleId());
            ruleGroup.setColumnCondition(generatedExpression);
            ruleGroup.setTenantCode(SecurityUtils.getTenantCode());
            updateYqFormRule(ruleGroup);
        }
        return false;
    }


    /**
     * 批量删除分单规则
     * 
     * @param ruleIds 需要删除的分单规则主键
     * @return 结果
     */
    @Override
    public int deleteYqFormRuleByRuleIds(String[] ruleIds)
    {
        return yqFormRuleMapper.deleteYqFormRuleByRuleIds(ruleIds);
    }

    /**
     * 批量删除分单规则
     *
     * @param ruleId 需要删除的分单规则主键
     * @return 结果
     */
    @Override
    public int deleteYqFormRuleByGroupRuleId(String ruleId)
    {
        return yqFormRuleMapper.deleteYqFormRuleByGroupRuleId(ruleId);
    }

    /**
     * 删除分单规则信息
     * 
     * @param ruleId 分单规则主键
     * @return 结果
     */
    @Override
    public int deleteYqFormRuleByRuleId(String ruleId)
    {
        return yqFormRuleMapper.deleteYqFormRuleByRuleId(ruleId);
    }

    @Override
    public List<YqFormRule> selectYqFormRuleByGroupId(String groupId) {
        return yqFormRuleMapper.selectYqFormRuleListByGroupId(groupId);
    }

    @Override
    public List<YqFormRule> selectYqFormRuleListByFormCode(String formCode, String tenantCode) {
        return yqFormRuleMapper.selectYqFormRuleListByFormCode(formCode,tenantCode);
    }
}
